package com.maple.soa.thread;

import java.util.concurrent.TimeUnit;

/**
 * 抢票
 * 多线程操作统一对象，线程不安全问题
 *
 * @author lijian
 * @date 2021/4/25 23:29
 */
public class T03_RobTicket {

    public static void main(String[] args) {
        Rob rob = new Rob();

        new Thread(rob, "张三").start();
        new Thread(rob, "李四").start();
        new Thread(rob, "王五").start();
    }
}

class Rob implements Runnable {
    private int ticketNum = 10;

    @Override
    public void run() {
        while (true) {
            if (ticketNum <= 0) {
                break;
            }
            // 模拟逻辑代码延时
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "==>抢到了第: " + ticketNum-- + "张票");
        }
    }
}
